package com.demo.utils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * 日期操作工具类
 */
public class DateUtil {

    public static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String DATE_MI_FORMAT = "yyyy-MM-dd HH:mm";
    public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String CH_DATE_FORMAT_YMD = "yyyy年MM月dd日";
    public static final String CH_DATE_FORMAT_MD = "MM月dd日";
    public static final String DATE_DI_FORMAT = "YYYYMMDDhhmmss";
    public static final long DAY_MILLI = 24 * 60 * 60 * 1000; // 一天的MilliSecond

    /**
     * 获取当前时间的指定格式
     *
     * @param format
     * @return
     */
    public static String getCurrDate(String format) {
        return dateToStr(new Date(), format);
    }

    /**
     * 时间戳转字符串--自定义格式
     *
     * @param time
     * @param format
     * @return
     */
    public static String timestampToString(Timestamp time, String format) {
        DateFormat sdf = new SimpleDateFormat(format);

        return sdf.format(time);
    }

    /**
     * yyyy-MM-dd 字符串转Date
     * @param dateStr
     * @return
     */
    public static Date strToDate(String dateStr) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);

            return sdf.parse(dateStr);
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * 自定义格式字符串转Date
     * @param dateStr
     * @return
     */
    public static Date strToDate(String dateStr, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);

            return sdf.parse(dateStr);
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * Date转字符串yyyy-MM-dd HH:mm:ss
     * @param date
     * @return
     */
    public static String dateToStr(Date date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_FORMAT);

            return sdf.format(date);
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * Date转自定义格式字符串
     * @param date
     * @param format
     * @return
     */
    public static String dateToStr(Date date, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);

            return sdf.format(date);
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * 获取Date的小时数
     *
     * @param date
     * @return
     */
    public static Integer getHour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 获取Date的分钟数
     *
     * @param date
     * @return
     */
    public static Integer getMinute(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MINUTE);
    }

    public  static String getNewDateSuffix(String suffix) {

        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            String data =  df.format(new Date());
            data+=suffix;


            return data;
        }catch (Exception e){
            e.printStackTrace();

        }
        return  null;




    }
    /**
     * 取得两个日期之间的日数
     *
     * @return t1到t2间的日数，如果t2 在 t1之后，返回正数，否则返回负数
     */
    public static long daysBetween(Date t1, Date t2) {
        return (t2.getTime() - t1.getTime()) / DAY_MILLI;
    }

    /**
     * 当前时间的字符串
     *
     * @param date
     *            时间
     * @return 时间的数字字符串格式
     */
    public static String mathString(Date date) {
        SimpleDateFormat time = new SimpleDateFormat("yyyyMMddHHmmss", Locale.UK);
        return time.format(date);
    }

    /**
     * 判断两个日期的大小
     * DATE1 > DATE2 返回1
     * DATE1 < DATE2返回-1
     * DATE1 = DATE2 返回0
     * @param date1
     * @param date2
     * @return
     */
    public static int compare_date(Date date1, Date date2) {
        try {
            if (date1.getTime() > date2.getTime()) {
                System.out.println("dt1 在dt2前");
                return 1;
            } else if (date1.getTime() < date2.getTime()) {
                System.out.println("dt1在dt2后");
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }

    /**
     * 判断两个日期的大小
     * DATE1 > DATE2 返回1
     * DATE1 < DATE2返回-1
     * DATE1 = DATE2 返回0
     * @param DATE1
     * @param DATE2
     * @return
     */
    public static int compare_date(String DATE1, String DATE2) {
        DateFormat df = new SimpleDateFormat(DATETIME_FORMAT);
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            int i = compare_date(dt1, dt2);
            return i;
        } catch (Exception exception) {
            exception.printStackTrace();
            return 0;
        }
    }


    /**
     * 获取指定日期
     * 1.获取以后的日期days传正数
     * 2.获取以前的日期days传负数
     * @param date
     * @param days
     * @return
     */
    public static String getSpecifiedDay(Date date, int days, String format){
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE,days);
        date=calendar.getTime();
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String dateString = formatter.format(date);
        return  dateString;
    }


    public static void main(String[] args) {


     // Data d =   DateUtil.getNewDateSuffix(" 00:00:00");



    }

}
